Skip to content

Borrow lists Enum keeps using, and share defrustmod mappings through aliases - #18

Merged
dannote merged 4 commits into
masterfrom
borrowing-enum-and-rust-modules
Oct 25, 2026
Merged

dannote merged 4 commits into
masterfrom
borrowing-enum-and-rust-modules

Conversation

@dannote

@dannote dannote commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Lets Figler write Enum, pipes, and comprehensions instead of hand-written iterator chains, and drop 107 repeated defrustmod lines.

  • Enum ownership. Enum functions took every owned list with into_iter(), so a list used again, or borrowed by a value built from it (by_id = records_by_id(records)), did not compile. Blocks now lower knowing which variables later expressions use and what each binding was built from; Enum takes a list only at its last use and otherwise iterates .iter(). Questions (any?, all?, count/2, empty?, sum) borrow regardless, items kept from a borrowed list (filter, reject, find, take, drop, reverse) are cloned, and numbers and atoms are copied out.
  • Constructor borrows. HashSet.new() (and new/with_capacity/default of the type a reference parameter names) passed where a reference is expected is borrowed.
  • One-value generators. for record <- records, {:some, id} <- [record_id(record)], ... matches the value beside the item instead of building a Vec per item, so it reads as filter_map.
  • Shared Rust module mappings. A module's defrustmod mappings are exported (__rustq_rust_modules__/0) and apply under any alias naming it; defrustmod __MODULE__, as: [...] maps the module itself. Aliases whose first segment names a RustQ module import it without waiting on nested modules the compiler may never define.

I first tried casing nested segments as Rust modules (Records.NodeChange → records::node_change), but Rust's casing is ambiguous there (PathBuilder.ArcSize.Large is a type then a variant), so kiwi_codec will group its constants as associated constants instead (kiwi_records::NodeChange::GUID).

Checked on Figler: inherit_property_definitions and records_by_id rewritten with Enum.any?, Enum.map, and a comprehension generate the same Rust as the hand-written chains (with &records instead of &&records) and pass cargo check.

Follow-up from converting Figler:

  • A list reached through a reference (binding.superseded for a borrowed binding) is never taken: Enum borrows it, and a comprehension copies its items, as it also does for an owned list still in use.
  • Enum.filter/reject/find/count lower to .filter/.find (clippy's unnecessary_filter_map), matching number and atom items as |&value| via a new AST.PatRef; slices iterate with iter() (into_iter_on_ref). Figler's generated crate passes cargo clippy -D warnings.

@dannote
dannote merged commit 6ca4f6d into master Oct 25, 2026
2 checks passed
@dannote
dannote deleted the borrowing-enum-and-rust-modules branch October 25, 2026 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant